﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections;

namespace Financial
{
    public partial class MoreInformation : Form
    {
        private Quote quote;
       
        private const String CHART_URL_1D = "http://ichart.finance.yahoo.com/b?s=";
        private const String CHART_URL_5D = "http://ichart.finance.yahoo.com/w?s=";
        private const String CHART_URL_3M = "http://chart.finance.yahoo.com/c/3m/";
        private const String CHART_URL_6M = "http://chart.finance.yahoo.com/c/6m/";
        private const String CHART_URL_1Y = "http://chart.finance.yahoo.com/c/1y/";
        private const String CHART_URL_2Y = "http://chart.finance.yahoo.com/c/2y/";
        private const String CHART_URL_5Y = "http://chart.finance.yahoo.com/c/5y/";
        private const String CHART_URL_MAX = "http://chart.finance.yahoo.com/c/my/";

        public MoreInformation(Quote quote)
        {
            this.quote = quote;
            InitializeComponent();
            InitializeFormData();
            DisplayNewsStories();
            PopulateComboBox();
            this.Show();
        }

        private void InitializeFormData()
        {
            titleLabel.Text = this.quote.Name;
            DisplayQuotePicture(CHART_URL_3M);
        }

        private void DisplayNewsStories()
        {
            int count = 20;
            ArrayList stories = GetQuoteNewsList();
            for (int i = 0; i < stories.Count; i++)
            {
                NewsStory story = (NewsStory)stories[i];

                LinkLabel titleLabel = new LinkLabel();
                Label publishDate = new Label();

                titleLabel.Font = new Font(FontFamily.GenericSerif, 12);
                titleLabel.Location = new Point(10, count);
                titleLabel.AutoSize = true;
                titleLabel.Text = story.Title;
                titleLabel.LinkColor = Color.Blue;
                titleLabel.Links.Add(0, story.Link.Length, story.Link);
                titleLabel.LinkClicked += 
                        new LinkLabelLinkClickedEventHandler(titleLabel_LinkClicked);
                titleLabel.Enabled = true;

                publishDate.AutoSize = true;
                publishDate.Location = new Point(20, count + 25);
                publishDate.Text = story.pubDate;
                publishDate.ForeColor = Color.Black;

                this.newsPanel.Controls.Add(titleLabel);
                this.newsPanel.Controls.Add(publishDate);

                count += 55;
            }
        }

        void PopulateComboBox()
        {
            chartComboBox.Items.Add("One Day");
            chartComboBox.Items.Add("Five Days");
            chartComboBox.Items.Add("Three Months");
            chartComboBox.Items.Add("Six Months");
            chartComboBox.Items.Add("One Year");
            chartComboBox.Items.Add("Two Years");
            chartComboBox.Items.Add("Five Years");
            chartComboBox.Items.Add("Maximum");
            chartComboBox.SelectedItem = "Three Months";
        }

        void titleLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            String target = e.Link.LinkData as String;
            if (target != null)
            {
                System.Diagnostics.Process.Start(target);
            }
        }

        private void DisplayQuotePicture(String URL)
        {
            graphPicture.Image = GetQuotePicture(URL);
            graphPicture.Width = graphPicture.Image.Width;
            graphPicture.Height = graphPicture.Image.Height;
        }

        private Image GetQuotePicture(String URL)
        {
            String quotePictureURL = URL + this.quote.Symbol;
            WebRequest request = WebRequest.Create(quotePictureURL);
            WebResponse response = request.GetResponse();
            return Image.FromStream(response.GetResponseStream());
        }

        private ArrayList GetQuoteNewsList()
        {
            return NewsStory.CreateNewsStoryList(this.quote.Name);
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            String selectedItem = chartComboBox.Items[chartComboBox.SelectedIndex].ToString();
            switch (selectedItem)
            {
                case "One Day":
                    DisplayQuotePicture(CHART_URL_1D);
                    break;
                case "Five Days":
                    DisplayQuotePicture(CHART_URL_5D);
                    break;
                case "Three Months":
                    DisplayQuotePicture(CHART_URL_3M);
                    break;
                case "Six Months":
                    DisplayQuotePicture(CHART_URL_6M);
                    break;
                case "One Year":
                    DisplayQuotePicture(CHART_URL_1Y);
                    break;
                case "Two Years":
                    DisplayQuotePicture(CHART_URL_2Y);
                    break;
                case "Five Years":
                    DisplayQuotePicture(CHART_URL_5Y);
                    break;
                case "Maximum":
                    DisplayQuotePicture(CHART_URL_MAX);
                    break;
            }
        }
    }
}
